package com.web.servlet;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.IOUtils;

import com.web.constant.Constant;
import com.web.domain.Category;
import com.web.domain.Product;
import com.web.service.AdProductService;
import com.web.utils.BeanFactory;
import com.web.utils.UUIDUtils;
import com.web.utils.UploadUtils;

/**
 * Servlet implementation class AddProduct
 */
@WebServlet("/add_product") 
public class AddProduct extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			Map<String,Object> map = new HashMap<>();
			DiskFileItemFactory factory = new DiskFileItemFactory();
			ServletFileUpload up = new ServletFileUpload(factory);
			List<FileItem> list = up.parseRequest(request);
			for(FileItem fi :list) {
				String fileItem = fi.getFieldName();
				if(fi.isFormField()) {
					map.put(fileItem, fi.getString("utf-8"));
				}else {
					String name = fi.getName();
					String realName = UploadUtils.getRealName(name);
					InputStream is = fi.getInputStream();
					String uuidName = UploadUtils.getUUIDName(realName);
					
					String productReal = getServletContext().getRealPath("/products");
					//创建随机目录
				
					FileOutputStream os = new FileOutputStream(new File(productReal,uuidName));
					IOUtils.copy(is, os);
					
					os.close();
					is.close();
					
					fi.delete();
					
					map.put(fileItem, "products/"+uuidName);
				}
			}
			
			//封装Product对象
			Product product = new Product();
			Date date=new Date();                             
	         SimpleDateFormat temp=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
	         String date2=temp.format(date);  
	         Date date3=temp.parse(date2);  
			map.put("pdate", date3);
			map.put("pflag",Constant.PRODUCT_IS_UP);
			map.put("pid",UUIDUtils.getNumId());
			BeanUtils.populate(product, map);
			Category c = new Category();
			int cid = Integer.parseInt((String) map.get("cid"));
			c.setCid(cid);
			product.setCategory(c);
			AdProductService aps = (AdProductService) BeanFactory.getBean("AdProductService");
			aps.addProduct(product);
		
			response.sendRedirect(request.getContextPath()+"/adproduct?method=findByAdPage&pageNumber=1");
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("保存商品失败");
		}
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
